In many software applications, a design pattern known as an event/listener pattern is used to process input. Some typical implementations of event/listeners may use a listener manager to store a collection of listener objects. Conventional listener managers may use a linked list data structure to store listener objects. Linked lists may be used because add and remove operations in linked lists are fast, low cost operations. Arrays, a common alternative to linked lists, may take much more time and memory to run add and remove operations. Adding and removing elements to an array may require that array elements be copied or that the array be resized. Array resizing and array element copying are slow, memory expensive operations.
To inform a listener object that an event has happened, a notify operation is used to convey information relating to the event to the listener object. In the linked list implementation, the notify process iteratively goes through the linked list, reporting information relating to the event to listener objects. A listener object may then invoke an external process in response to the information. However, linked list listener managers may be constrained by potential race conditions. Because add and remove calls may temporarily break links, all three operations (add, remove, and notify) may be required to be written into a synchronized block. This may prevent notify processes from running in parallel. In high performance machines, this may reduce performance.
To further illustrate conventional listener managers, Java® code describing one typical listener manager implementation is provided below. While the example code is written in the Java® programming language, it is to be appreciated that event listeners are also used in other programming languages. Note that all three functions (addListener, removeListener, and notify) use a synchronization qualifier to prevent other functions from operating in parallel.
class ListenerManager{   LinkedList listeners;   //Adds a listener to the list of registered listeners.   synchronized void addListener(Listener 1)   {   listeners.add(1);   }   //Removes a listener from the list of registered listeners.   synchronized void removeListener(Listener 1)   {   listeners.remove(1);   }   //Notify all the registered listeners.   synchronized void notify(EventObject e)   {      ListIterator iterator = listeners.listIterator( );      while(iterator.hasNext( ))         ((Listener)iterator.next( )).handleEvent(e);   }}